﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace Viewer.BusinessObject.BusinessEntities
{
    public partial class CHToChucHanhChinh : BusinessBase
    {

        #region Additional Propties
        public string TenToChucCha
        {
            get
            {
                if (this.ToChucHanhChinhIDCha == 0) return "Trung ương";
                return GetByID(this.ToChucHanhChinhIDCha).TenToChucHanhChinh;
            }
        }

        public string TenLoaiDonViHanhChinh
        {
            get
            {
                if (this.LoaiToChucHanhChinh == 0) return string.Empty;
                return (new CommonCode()).LoadCommonCodeByCodeId(this.LoaiToChucHanhChinh).CodeValue;
            }



        }
        #endregion

        public  List<CHToChucHanhChinh> GetAll()
        {
                return Context.CHToChucHanhChinhs.ToList();
        }

        public List<CHToChucHanhChinh> GetToChucByToChucCha(int pToChucChaID)
        {
           
            return Context.CHToChucHanhChinhs.Where(m => m.ToChucHanhChinhIDCha == pToChucChaID).ToList();
        }

        
        public CHToChucHanhChinh GetByID(int pToChucHanhChinhID)
        {
            return this.Context.CHToChucHanhChinhs.Where(item => item.ToChucHanhChinhID == pToChucHanhChinhID).FirstOrDefault();
        }

        public void Save()
        {
            //Add New
            if (this.ToChucHanhChinhID == 0)
            {
                this.Context.CHToChucHanhChinhs.InsertOnSubmit(this);
                this.Context.SubmitChanges();
            }
            else
            { 
                //Update
                var item = this.GetByID(this.ToChucHanhChinhID);
                item.ToChucHanhChinhIDCha = this.ToChucHanhChinhIDCha;
                item.TenToChucHanhChinh = this.TenToChucHanhChinh;
                item.ToChucHanhChinhLevel = this.ToChucHanhChinhLevel;
                item.GhiChu = this.GhiChu;
                item.LoaiToChucHanhChinh = this.LoaiToChucHanhChinh;
                this.Context.SubmitChanges();
            }
        }

        public void Delete()
        {
            var oItem = GetByID(this.ToChucHanhChinhID);
            this.Context.CHToChucHanhChinhs.DeleteOnSubmit(oItem);
            this.Context.SubmitChanges();
        }

        public static CHToChucHanhChinh_Report Report(int pToChucID)
        {

            using (DataClassesDataContext Context = new DataClassesDataContext())
            {
                return Context.sp_GetCHToChucHanhChinh(pToChucID);
            }

        }
    }
}